<?php

include_once('Zend/Search/Lucene.php');
include_once(dirname(dirname(__file__)) . '/includes/Tag.php');

$search = $_GET["q"];
$tag = $_GET["t"];

if ($search) {

	$index_path = dirname(dirname(dirname(__FILE__))) . '/search/tmp/index';
	$index = Zend_Search_Lucene::open($index_path);
	
	$query = Zend_Search_Lucene_Search_QueryParser::parse($search);
	
	//Perform a basic query
	$hits = $index->find($query);
	
	//For each hit, retreive the originating URL
	foreach($hits as $hit)
	{
		$doc = $hit->getDocument();
		
		if($hit->score >= $this->score_threshold)
		{
			$body = $doc->getFieldValue('body');
			$len = strlen($body);
			$out[$hit->id] = array(
				'score' => $hit->score,
				'url' => $doc->getFieldValue('url'),
				'title' => $doc->getFieldValue('title'),
				'body' => '...' . substr(htmlspecialchars($body), round($len/2), 300) . '...'
			);
		}
	}
	
} elseif ($tag) {
	$theTag = new Tag();
	$theTag->loadBy("name = ?", $tag);
	
	ActiveRecord::$selectTableMappings["Page"] = "page_view";
	
	if ($theTag->id) {
		$pages = $theTag->loadPages();
		
		if ($pages) {
			Page::loadContentsForPages($pages);
			
			foreach ($pages as $page) {
				$maxLength = 0;
				foreach ($page->contentObj as $field => $content) {
					//echo strlen($content->value) . " > " . $maxLength;
					if ($content->revision_id == $content->page_id && strlen($content->value) > $maxLength) {
						$maxLength = strlen($content->value);
						$maxContent = $content;
					}
				}
				$page->snippet = substr(strip_tags($maxContent->value), 0, 300) . "...";
			}
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $search ? "Search Results" : ($tag ? "Tag Results" : "Results not Found") ?></title>
<link href="/style/core.css" rel="stylesheet" type="text/css" />
<link href="/style/sub.css" rel="stylesheet" type="text/css" />
<script src="/scripts/sifr.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
	<h2 id="logo"><a href="/">The RBL Group</a></h2>
	<div id="searchNav">
		<form id="searchForm" action="/results">
			<input type="text" name="q" value="<?=$search?>" /> 
			<a id="search" href="javascript:document.getElementById('searchForm').submit();">SEARCH |</a>
		</form>
		<a id="contact" href="/contactus">CONTACT US</a>        
	 </div>
		
	<form id="goTab" onsubmit="location.href = document.getElementById('quickLinks').value;return false;">
		<label>I want to...</label>
			<select id="quickLinks" _cascade="quick_links">
				<option>Sign up for the newsletter.</option>
			</select>
			<a href="javascript:location.href = document.getElementById('quickLinks').value">GO</a>
	</form>
	
	<div id="content">
		<div id="subPage_header">&nbsp;</div>
		<div id="nav" _cascade="main_links">
			<ul>
				<li><b>ABOUT US</b></li>
				<li><a href="/our-people.php">Our People</a></li>
				<li><a href="/featured-clients.php">Featured Clients</a></li>
				<li><a href="/news-media.php">News &amp; Media</a></li>
				<li><a href="/rbl-story.php">The RBL Story</a></li>
			</ul>
			<ul>
				<li><b>FOCUS AREAS</b></li>
				<li><a href="/strategic-hr.php">Strategic HR</a></li>
				<li><a href="/leadership-brand">Leadership Brand</a></li>
				<li><a href="/global-presence.php">Global Presence</a></li>
			</ul>
			<ul>
				<li><b>SERVICES</b></li>
				<li><a href="/education-programs.php">Education Programs</a></li>
				<li><a href="/consulting.php">Consulting</a></li>
				<li><a href="/speakers.php">Speakers</a></li>
			</ul>
			<ul>
				<li><b>RESOURCES</b></li>
				<li><a href="/books-articles.php">Books &amp; Articles</a></li>
				<li><a href="/research.php">Research</a></li>
				<li><a href="/online-tools.php">Online Tools</a></li>
				<li><a href="/events.php">Events</a></li>
			</ul>
			<ul>
				<li><b>RBL INSTITUTE</b></li>
				<li><a href="/learn-more.php">Learn More</a></li>
				<li><a href="http://www.rblinstitute.com/">Login</a></li>
			</ul>
		</div>
		
		<div id="subContent" _if="$search">
			 <h1>Search Results for '<?php echo $search; ?>'</h1>
			 <p class="hr"></p>
             <div _if="$out" _foreach="$out as $result">
             	<h4><a href="<?php print($result['url']); ?>" _echo="$result['title']">Page Result</a></h4>
             	<p _echo="$result['body']"></p>
             </div>
             <p _else="">There were no results found for <strong _echo="$search">your search</strong></p>
		</div>
		
		<div id="subContent" _else="">
			 <h1>Tag Results for '<?php echo $tag ?>'</h1>
			 <p class="hr"></p>
             <div _if="$pages" _foreach="$pages as $page">
             	<h4><a href="<?php print(preg_replace('/^[^\/]+/', '', $page->path)); ?>" _echo="$page->contents['title']">Page Result</a></h4>
             	<p _echo="$page->snippet"></p>
             </div>
             <p _else="">There were no results found for <strong _echo="$tag">your search</strong></p>
		</div>
		
		
		<div id="footer" _cascade="footer">
			<a href="/">Home</a> | <a href="#">Contact Us</a> | <a href="#">Sitemap</a> | <a href="#">Help</a> | <a href="#">Whatever</a> | <a href="/featured-clients.php">Clients</a><br />
			<span>2007 The RBL Group - All rights reserved</span>        
		</div>
	</div>
	<span id="shadow_left">&nbsp;</span>
	<span id="shadow_right">&nbsp;</span>
</div>
<script type="text/javascript">
//<![CDATA[
/* Replacement calls. Please see documentation for more information. */

if(typeof sIFR == "function"){

// This is the preferred "named argument" syntax
	sIFR.replaceElement(named({sSelector:"h3", sFlashSrc:"/flash/sifr.swf", sColor:"#666666"}));
	sIFR.replaceElement(named({sSelector:"h1", sFlashSrc:"/flash/sifr.swf", sColor:"#666666", sWmode:"transparent"}));

}
</script>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2676624-1";
urchinTracker();
</script>
</body>
</html>
